/**
 * 安全警报 编辑页
 *
 * 作者 zhangwei
 * 日期 2024-08-22 09:19:50
 */
layui.use(['jquery','laydate','febs', 'form', 'validate','commonJS','fileUpload','tinymce'], function () {
    var $ = layui.$,
        febs = layui.febs,
        laydate = layui.laydate,
        layer = layui.layer,
        form = layui.form,
        fileUpload = layui.fileUpload,
        commonJS = layui.commonJS,
        tinymce = layui.tinymce,
        $view = $('#accidentAlerts-edit'),
        validate = layui.validate;
    let accidentAlertsFileList = [];

    var tenantId = POPUP_DATA.tenantId;
    form.verify(validate);
    initFormValue();
    form.render();

    laydate.render({
        elem: "#publishDate",
        type: 'date',
    })
    laydate.render({
        elem: "#deadlineDate",
        type: 'date',
    })

    tinymce.init({
        selector: "#alertsContent",
        images_upload_url: '',
        image_title: false, // 是否开启图片标题设置的选择，这里设置否
        automatic_uploads: false,
        file_picker_types: 'image',
        relative_urls: false,
        remove_script_host: false,
        convert_urls: false,
        images_reuse_filename: true,
        height: 700,
        data: {}
    });

    fileUpload.render({
        elem:'#uploadBtn',
        url:ctx + 'accident/upload',
        type:'2',
        maxNum:5,
        defaultData: accidentAlertsData && accidentAlertsData.accidentAlertsFileList ? accidentAlertsData.accidentAlertsFileList:[],
        done:function(res){
        }
        ,error: function(){
            layer.msg("服务器异常")
        }
    })

    function initFormValue() {
        if (accidentAlertsData) {

            accidentAlertsFileList = accidentAlertsData.accidentAlertsFileList;
            form.val("accidentAlerts-edit-form", {
                "alertsId": accidentAlertsData.alertsId,
                "tenantId": accidentAlertsData.tenantId,
                "alertsType": accidentAlertsData.alertsType,
                "alertsTitle": accidentAlertsData.alertsTitle,
                "publishDate": accidentAlertsData.publishDate,
                "deadlineDate": accidentAlertsData.deadlineDate,
                "alertsContent":accidentAlertsData.alertsContent,
                "topFlg": accidentAlertsData.topFlg == '1'? true:false,
                "status": accidentAlertsData.status == '1'? true:false,
            });
            /*if(accidentAlertsFileList && accidentAlertsFileList.length>0){
                // $("#uploadBtn").hide();
                $.each(accidentAlertsFileList,function (index,file){
                    $("#showFiles").append(' <div class="file-flex" id="item'+index+'"><span class="file-item fileName" onclick="downloadAlertsFile(this)" filePath="'+file.filePath+'" fileName="'+file.fileName+'">' + file.fileName + '</span><div class="deleteBtn" fileId="'+file.alertsFileId+'" id="'+index+'" onclick="deleteFile(this)">x</div></div>')
                    // $("#showFiles").append( '<div class="fileOne" style="display: flex;width: 100%;position: relative;margin-bottom: 10px;" data-filename="'+ file.fileName +'" data-filepath="'+ file.filePath +'"><div style="width: calc(100% - 125px);white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">'+file.fileName+'</div><div class="deleteBtn" fileId="'+file.alertsFileId+'" id="'+index+'" onclick="deleteFile(this)">x</div></div>');
                })
            }*/
        }
    }

    form.on('submit(accidentAlerts-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        data.tenantId = tenantId;
        if(data.status == 'on'){
            data.status = "1";
        }else{
            data.status = "0";
        }
        if(data.topFlg == 'on'){
            data.topFlg = "1";
        }else{
            data.topFlg = "0";
        }
        // data.alertsContent = tinyMCE.editors["alertsContent"].getContent();
        data.accidentAlertsFileList = getFileData();
        if(commonJS.isEmpty(data.alertsContent.trim()) ){
            febs.alert.warn("警报内容不能为空！")
            return false;
        }
        console.log(data);
        febs.modal.confirm('保存提示', "是否保存安全警报信息？",function (index) {
            if (data.alertsId && data.alertsId.length > 0) {
                febs.postArray(ctx + 'accidentAlerts/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-accidentAlerts').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'accidentAlerts', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-accidentAlerts').find('#query').trigger('click');
                });
            }
        })
    }

    window.downloadAlertsFile = function (_this) {
        let filePath = $(_this).attr("filePath");
        let fileName = $(_this).attr("fileName");
        let data = {
            filePath:filePath,
            fileName:fileName
        };
        febs.download(ctx + 'accidentFile/downloadFile', data, fileName);
    }

    function getFileData(){
        var fileDatas = $view.find(".fileOne")
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            fileList.push(data);
        })
        return fileList;
    }

});